//
//  SRPickerView.m
//  PartTimeJob
//
//  Created by 孙锐 on 2017/8/8.
//  Copyright © 2017年 孙锐. All rights reserved.
//

#import "SRPickerView.h"
#define kPickerHeight 216

@interface SRPickerView ()<UIPickerViewDelegate,UIPickerViewDataSource>

@end

typedef void (^btnBlock)(void);
typedef void (^dateBlock)(NSDate *);

typedef void (^cancelPickerBlock)(void);
typedef void (^surePickerBlock)(NSString *, int);

@implementation SRPickerView{

    dateBlock sureBlock;
    btnBlock cancelBlock;
    UIDatePicker *datePicker;
    
    cancelPickerBlock cancelPicker;
    surePickerBlock surePicker;
    UIPickerView *pickView;
    int numberOfComponents;
    NSDictionary *infoDict;
    NSArray *infoArr;
}


- (instancetype)initWithFrame:(CGRect)frame BackColor:(UIColor *)color AndTitle:(NSString *)text AndUIDatePickerMode:(UIDatePickerMode)mode AndTimeInterval:(int)timeInterval AndSureBlock:(void (^)(id date))sure AndCancelBlock:(void (^)(void))cancel
{
    self = [super init];
    if (self) {
        self.frame = frame;
        self.layer.cornerRadius = 30*ScreenScale;
        self.layer.masksToBounds = YES;
        
        self.backgroundColor = [UIColor whiteColor];
        UIView *colorView = [[UIView alloc]init];
        colorView.backgroundColor = color;
        colorView.frame = CGRectMake(0, 0, kScreenWidth, 124*ScreenScale);
        [self addSubview:colorView];
        sureBlock = sure;
        cancelBlock = cancel;
        
        UIButton * cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        cancelBtn.frame = CGRectMake(0, 0, 0.2*kScreenWidth, 0.1*kScreenWidth);
        [cancelBtn setTitleColor:UIColorFromRGB(0xFFFFFF) forState:UIControlStateNormal];
        [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
        cancelBtn.titleLabel.font = [UIFont systemFontOfSize:0.0378*kScreenWidth];
        [cancelBtn addTarget:self action:@selector(cancelChoose) forControlEvents:UIControlEventTouchUpInside];
        
        UIButton * okBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        okBtn.frame = CGRectMake(0.8*kScreenWidth, 0, 0.2*kScreenWidth, 0.1*kScreenWidth);
        [okBtn setTitleColor:UIColorFromRGB(0xFFFFFF) forState:UIControlStateNormal];
        okBtn.titleLabel.font = [UIFont systemFontOfSize:0.0378*kScreenWidth];
        [okBtn setTitle:@"确定" forState:UIControlStateNormal];
        [okBtn addTarget:self action:@selector(sureChoose) forControlEvents:UIControlEventTouchUpInside];

        [self addSubview:okBtn];
        [self addSubview:cancelBtn];
        
        UILabel *middleLabel = [[UILabel alloc]init];
        middleLabel.textColor = [UIColor whiteColor];
        middleLabel.font  =[UIFont systemFontOfSize:0.0378*kScreenWidth];
        middleLabel.frame = CGRectMake(0.2*kScreenWidth, 0, 0.6*kScreenWidth, 124*ScreenScale);
        middleLabel.text = text;
        middleLabel.textAlignment = NSTextAlignmentCenter;
        [self addSubview:middleLabel];
        
        datePicker = [ [ UIDatePicker alloc] initWithFrame:CGRectMake(0,0.1*kScreenWidth,kScreenWidth,kPickerHeight)];
        datePicker.datePickerMode = mode;
        datePicker.minuteInterval = timeInterval;

        [self addSubview:datePicker];
        
    }
    return self;
}

-(void)cancelChoose{

    cancelBlock();
}


-(void)sureChoose{
    
    sureBlock(datePicker.date);
}

-(void)setDate{
    if (self.maxDate) {
        datePicker.maximumDate = self.maxDate;
    }
    if (self.minDate) {
        datePicker.minimumDate = self.minDate;
    }
    if (self.defaultDate) {
        datePicker.date = self.defaultDate;
    }
}




- (instancetype)initWithFrame:(CGRect)frame BackColor:(UIColor *)color AndTitle:(NSString *)text AndSureBlock:(void (^)(id string, int num))sure AndCancelBlock:(void (^)(void))cancel AndComponentsNumber:(int)num AndInfo:(NSArray *)arr{
    self = [super init];
    if (self) {
        self.frame = frame;
        self.layer.cornerRadius = 30*ScreenScale;
        self.layer.masksToBounds = YES;
        self.backgroundColor = [UIColor whiteColor];
        UIView *colorView = [[UIView alloc]init];
        colorView.backgroundColor = color;
        colorView.frame = CGRectMake(0, 0, kScreenWidth, 124*ScreenScale);
        [self addSubview:colorView];

        surePicker = sure;
        cancelPicker = cancel;
        numberOfComponents = num;
        infoArr = [NSArray arrayWithArray:arr];
        UIButton * cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        cancelBtn.frame = CGRectMake(0, 0, 0.2*kScreenWidth, 0.1*kScreenWidth);
        [cancelBtn setTitleColor:UIColorFromRGB(0xFFFFFF) forState:UIControlStateNormal];
        [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
        cancelBtn.titleLabel.font = [UIFont systemFontOfSize:0.0378*kScreenWidth];
        [cancelBtn addTarget:self action:@selector(cancelPicker) forControlEvents:UIControlEventTouchUpInside];
        
        UIButton * okBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        okBtn.frame = CGRectMake(0.8*kScreenWidth, 0, 0.2*kScreenWidth, 0.1*kScreenWidth);
        [okBtn setTitleColor:UIColorFromRGB(0xFFFFFF) forState:UIControlStateNormal];
        okBtn.titleLabel.font = [UIFont systemFontOfSize:0.0378*kScreenWidth];
        [okBtn setTitle:@"确定" forState:UIControlStateNormal];
        [okBtn addTarget:self action:@selector(choosePicker) forControlEvents:UIControlEventTouchUpInside];
        
        [self addSubview:okBtn];
        [self addSubview:cancelBtn];

        UILabel *middleLabel = [[UILabel alloc]init];
        middleLabel.textColor = [UIColor whiteColor];
        middleLabel.font  =[UIFont systemFontOfSize:0.0378*kScreenWidth];
        middleLabel.frame = CGRectMake(0.2*kScreenWidth, 0, 0.6*kScreenWidth, 124*ScreenScale);
        middleLabel.text = text;
        middleLabel.textAlignment = NSTextAlignmentCenter;
        [self addSubview:middleLabel];
        
        pickView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 124*ScreenScale, kScreenWidth, 216)];
        pickView.backgroundColor = [UIColor whiteColor];
        pickView.dataSource = self;
        pickView.delegate = self;
        for (NSDictionary *dic in infoArr) {
            [pickView selectRow:0 inComponent:[dic[@"num"] intValue] animated:NO];
        }
        [self addSubview:pickView];
        
    }
    return self;
}

-(void)cancelPicker{

    cancelPicker();
}

-(void)choosePicker{

    NSString *string = @"";
    int selectNum = 0;
    for (int i=0; i<infoArr.count; i++) {
        NSDictionary *dict = infoArr[i];
        selectNum = (int)[pickView selectedRowInComponent:[dict[@"num"] intValue]];
        string = [string stringByAppendingString:dict[@"info"][selectNum]];
    }
    
    surePicker(string ,selectNum);
}

// returns the number of 'columns' to display.
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{

    return numberOfComponents;
}

// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{

    for (infoDict in infoArr) {
        if ((int)component == [infoDict[@"num"] intValue]) {
            return [infoDict[@"info"] count];
        }
    }
 
    return 0;
    
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    for (infoDict in infoArr) {
        if ((int)component == [infoDict[@"num"] intValue]) {
            return infoDict[@"info"][row];
        }
    }
    return @"";
}

-(void)selecctRow:(int)row{
    [pickView selectRow:row inComponent:0 animated:NO];
}



@end
